Skip to content

feat(integrations): Add http.route attribute to server spans - #7183

Open
mjq wants to merge 2 commits into
masterfrom
mjq/http-route-server-spans
Open

feat(integrations): Add http.route attribute to server spans#7183
mjq wants to merge 2 commits into
masterfrom
mjq/http-route-server-spans

Conversation

@mjq

@mjq mjq commented Aug 12, 2026

Copy link
Copy Markdown
Member

Description

Currently the HTTP path template is only available in the span name. Make it available as a semantic attribute as well (http.route - definition in conventions).

This will also let us use this in e.g. HTTP server span description generation, which will be necessary to maintain consistent descriptions between transactions and span streaming.

Issues

@linear-code

linear-code Bot commented Aug 12, 2026

Copy link
Copy Markdown

PY-2732

Comment thread sentry_sdk/scope.py Outdated
@github-actions

github-actions Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Codecov Results 📊

118671 passed | ⏭️ 6732 skipped | Total: 125403 | Pass Rate: 94.63% | Execution Time: 413m 31s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +20
Passed Tests 📈 +20
Failed Tests
Skipped Tests

All tests are passing successfully.

✅ Patch coverage is 98.15%. Project has 2478 uncovered lines.
✅ Project coverage is 90.29%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
sentry_sdk/integrations/asgi.py 80.00% ⚠️ 1 Missing and 1 partials
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    90.23%    90.29%    +0.06%
==========================================
  Files          193       193         —
  Lines        25482     25527       +45
  Branches      9384      9408       +24
==========================================
+ Hits         22994     23049       +55
- Misses        2488      2478       -10
- Partials      1431      1430        -1

Generated by Codecov Action

Comment thread sentry_sdk/integrations/asgi.py
@mjq
mjq force-pushed the mjq/http-route-server-spans branch from dddf367 to b1363a5 Compare August 13, 2026 20:16
Comment thread sentry_sdk/integrations/starlette.py
mjq added a commit that referenced this pull request Aug 24, 2026
…#7201)

The sync request/response handler passed the _isolation_ scope to
`_set_transaction_name_and_source`, but the transaction/segment span
lives on the _current_ scope. As a result the route-resolved name never
reached the span for sync endpoints, which were instead named by the raw
URL from the ASGI middleware (`transaction_info.source` of `url` rather
than `route`). Async handlers already used the current scope and were
unaffected.

Pass the current scope (already computed above) so sync and async
handlers behave identically:
- streaming: the segment name / `sentry.segment.name.source` are
route-based
- static: the transaction event name / source are route-based

For parametrized routes this also removes high-cardinality URL
transaction names for sync endpoints.

Found while working on
#7183.
sentrivana pushed a commit that referenced this pull request Aug 25, 2026
…#7201)

The sync request/response handler passed the _isolation_ scope to
`_set_transaction_name_and_source`, but the transaction/segment span
lives on the _current_ scope. As a result the route-resolved name never
reached the span for sync endpoints, which were instead named by the raw
URL from the ASGI middleware (`transaction_info.source` of `url` rather
than `route`). Async handlers already used the current scope and were
unaffected.

Pass the current scope (already computed above) so sync and async
handlers behave identically:
- streaming: the segment name / `sentry.segment.name.source` are
route-based
- static: the transaction event name / source are route-based

For parametrized routes this also removes high-cardinality URL
transaction names for sync endpoints.

Found while working on
#7183.
@mjq
mjq force-pushed the mjq/http-route-server-spans branch from b1363a5 to 1d15a6d Compare August 26, 2026 20:10
@mjq mjq changed the title fix(spans): Add http.route attribute to HTTP server spans feat(integrations): Add http.route attribute to server spans Aug 26, 2026
Comment thread sentry_sdk/integrations/starlette.py
Comment thread sentry_sdk/scope.py
for attribute, value in attributes.items():
self.set_attribute(attribute, value)

def set_segment_attribute(self, key: str, value: "AttributeValue") -> None:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seemed like a useful helper function to have (and avoids duplicating this code in every HTTP integration), but I would also understand if we don't want to expand the API surface with this: it makes the "segment" name a public thing which AIUI we are trying to avoid?

Comment thread sentry_sdk/integrations/starlette.py
sentrivana pushed a commit that referenced this pull request Aug 27, 2026
### Description

When host routing, we don't have a route and instead fall back to the
request path for the transaction name. This should be given transaction
source `url` (for raw URLs) instead of the current value of `route` (for
parameterized routes).

This matters for #7183, where the mis-categorized transaction source
would lead to bad data in `http.route`. (Bug found by Cursor in
#7183 (comment)
when reviewing that PR).
@mjq
mjq marked this pull request as ready for review August 27, 2026 18:42
@mjq
mjq requested a review from a team as a code owner August 27, 2026 18:42

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit fdd83b4. Configure here.

if http_route is not None:
current_span._segment.set_attribute(
SPANDATA.HTTP_ROUTE, http_route
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aiohttp skips scope transaction update

Medium Severity

Switching to traces.get_current_span() makes the streaming branch actually run, but that path only mutates the span and never calls set_transaction_name. Error events still read the scope’s _transaction, which stays at the generic fallback, so captured exceptions can be grouped and linked under the wrong name.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit fdd83b4. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

http.route missing from HTTP server spans

1 participant